package com.ygsoft.tianlu_mall.service.impl;

import com.ygsoft.tianlu_mall.mapper.*;
import com.ygsoft.tianlu_mall.pojo.*;
import com.ygsoft.tianlu_mall.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public List<User> getAll(){
        return userMapper.findAllUser();
    }
    @Override
    public void saveUser(User user) {
        userMapper.insert(user);
    }
    @Override
    public void updateUser(User user){
        userMapper.updateUser(user);
    }


    @Override
    public void deleteByPrimaryKey(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }


    @Override
    public List<User> getUsersByLike(String user_phone) {
        return userMapper.getUsersByLike(user_phone);
    }

    @Override
    public List<User> getUsersByPhone(String user_phone) {return userMapper.getUsersByPhone(user_phone);}

    @Override
    public User findByUserName(String user_name) {return userMapper.findByUserName(user_name);}

    @Override
    public User findbyusername(String user_name) {return userMapper.findbyusername(user_name);}

    @Override
    public int getRoleId(int userid) {return userRoleMapper.getRoleId(userid);}

    @Override
    public int getshopid(int userid) {return shopMapper.getshopid(userid);}

    @Override
    public List<User> getDetailById(int user_id) {return userMapper.getDetailById(user_id);}

    @Override
    public void add(User user){userMapper.add(user);}

    @Override
    public void addRole(int userid){userRoleMapper.addRole(userid);}

    @Override
    public void doRecharge(int user_id, BigDecimal recharge) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("recharge",recharge);
        userMapper.doRecharge(map);
    }

    @Override
    public void doCash(int user_id, BigDecimal cash){
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("cash",cash);
        userMapper.doCash(map);
    }

    @Override
    public void setNewSalt(int user_id, String newSalt){
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("newSalt",newSalt);
        userMapper.setNewSalt(map);
    }

    @Override
    public void setNewPassword(int user_id, String newUser_password){
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("newUser_password",newUser_password);
        userMapper.setNewPassword(map);
    }

    @Override
    public String getOldSalt(int user_id){return userMapper.getOldSalt(user_id);}

    @Override
    public String selectSystemUserPassword(int user_id){return userMapper.selectSystemUserPassword(user_id);}

    @Override
    public void updatePassword(String user_phone,String oldpassword,String user_password,String cfgPwd){
        //1.对参数进行非空验证
        if(oldpassword==null || oldpassword.length()==0) {
            throw new IllegalArgumentException("请输入旧密码！");
        }
        if(user_password==null || user_password.length()==0) {
            throw new IllegalArgumentException("请输入新密码！");
        }
        if(user_password==null || user_password.length()==0) {
            throw new IllegalArgumentException("请确认新密码！");
        }
        //2.验证参数的合法性、
        //2.1判断两次输入的新密码是否一致
        if(!user_password.equals(cfgPwd)) {
            throw new IllegalArgumentException("两次输入密码不一致！");
        }
        //2.2判断旧密码是否跟登录密码一致
        //2.2.1 先获取用户的登陆身份通过shiro认证管理工具获得
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        SimpleHash sh = new SimpleHash("MD5", oldpassword, user.getSalt());
        String sh = new Md5Hash(oldpassword,user.getSalt(),1024).toString();
        //sh.toHex()是将加密后的密码转换为16进制
        if(!user.getUser_password().equals(sh)) {
            throw new IllegalArgumentException("输入的旧密码不正确！");
        }
        //3.更新密码
        String salt = new SecureRandomNumberGenerator().nextBytes().toHex(); //生成盐值
        String ciphertext = new Md5Hash(user_password,salt,1024).toString(); //生成的密文

        HashMap<String,Object> map = new HashMap<>();
        map.put("user_phone",user_phone);
        map.put("salt",salt);
        map.put("ciphertext",ciphertext);
        userMapper.updatePassword(map);


    }

    @Override
    //根据用户名查找用户角色
    public Set<String> findRoles(String user_name){
        Set<String> roleNameSet = new HashSet<>();
        Integer user_id = findbyusername(user_name).getId();
        List<UserRole> UserRoleList = userRoleMapper.findByUserId(user_id);
        for(int i = 0; i < UserRoleList.size(); i++){
            Role role = roleMapper.findById(UserRoleList.get(i).getUser2role_id());
            roleNameSet.add(role.getRoleName());
        }
        return roleNameSet;
    }

    //根据用户名查找用户权限
    public Set<String> findPermissions(String user_name){
        Set<String> permissionNameSet = new HashSet<>();
        Integer user_id = findbyusername(user_name).getId();
        List<Integer> roleIdList = new ArrayList<>();
        List<UserRole> usersRoleList = userRoleMapper.findByUserId(user_id);
        for(int i = 0; i < usersRoleList.size(); i++){
            Integer roleId = roleMapper.findById(usersRoleList.get(i).getUser2role_id()).getRole_id();
            roleIdList.add(roleId);
        }
        List<RolePermission> rolePermissionList = rolePermissionMapper.findByRoleIdIn(roleIdList);
        for(int i = 0;i<rolePermissionList.size();i++){
            Permission permission = permissionMapper.findById(rolePermissionList.get(i).getRolepermission_id());
            permissionNameSet.add(permission.getPermission_name());
        }
        return permissionNameSet;
    }

    @Override
    public List checkUser(String user_name){
        return userMapper.checkUser(user_name);
    }

    @Override
    public String getUserName(int user_id) {return userMapper.getUserName(user_id);}

    @Override
    public String getUsersTieleUrl(int user_id) {return userMapper.getUsersTieleUrl(user_id);}

    @Override
    @Transactional
    public void saveUsersTitleUrl(int user_id, String titleUrl) {
        HashMap<String,Object> urlMap = new HashMap<>();
        urlMap.put("user_id",user_id);
        urlMap.put("user_pic",titleUrl);
        userMapper.saveUsersTitleUrl(urlMap);
    }

//    @Override
//    public void deleteList(ids) {
//        EmployeeExample example = new EmployeeExample();
//        EmployeeExample.Criteria criteria = example.createCriteria();
//        //delete from xxx where emp_id in(1,2,3)
//        criteria.andEmpIdIn(ids);
//        userMapper.deleteList(example);
//    }

}
